<?php
require_once (dirname ( __FILE__ ) . '/guestbook/guestbook.inc.php');
$api = trim ( $_REQUEST ['api'] );
$page_count = 10;

if (empty ( $api )) {
	_error ( 'error' );
}
//留言列表
if ($api == 'guestbooklist') {
	$page = intval ( $page );
	if (! $page)
		$page = 1;
	$totalrows = $dsql->GetOne ( 'SELECT COUNT(*) as count FROM `#@__guestbook` WHERE isaudit=1' );
	$total = $totalrows ['count'];

	$mypage = ceil ( $total / $page_count );
	$sql = 'SELECT uname,id,dtime,face,msg,remsg FROM `#@__guestbook` WHERE isaudit=1 ORDER BY id DESC LIMIT ' . (($page - 1) * $page_count) . "," . $page_count;

	$dsql->Execute ( 'me', $sql );
	while ( $row = $dsql->GetArray ( 'me' ) ) {
		$row ['date'] = MyDate ( 'Y-m-d H:i', $row ['dtime'] );
		$html .= '<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">';
		$html .= '<tr>';
		$html .= '<td width="160" align="center" class="ltd"><b>' . $row ['uname'] . '</b> </td>';
		$html .= '<td height="26" class="timetd"><img height="16" src="/plus/guestbook/images/time.gif" width="16" /> 时间: ' . $row ['date'] . ' &nbsp;';
		$html .= '</td>';
		$html .= '</tr>';
		$html .= '<tr>';
		$html .= '<td width="160" rowspan="2" align="center" class="lefttd"><table width="160" border="0" align="center" cellpadding="1" cellspacing="2">';
		$html .= '<tr>';
		$html .= '<td height="80" align="center"><img src="/plus/guestbook/images/' . $row ['face'] . '.gif" border=0 /> </td>';
		$html .= '</tr>';
		$html .= '</table></td>';
		if ($row ['remsg']) {
			$html .= '<td class="msgtd" style="background:#fff;">';
			$html .= '<div class="rebox">' . $row ['msg'] . '</div>';
			$html .= '<div id="reboxinfo" style="line-height:25px;">';
			$html .= '<div id="tomsg_' . $row ['id'] . '" style="height:75px;overflow:hidden; ">';
			$html .= '<font color=red>印奈儿客服回复：</font><br />' . $row ['remsg'] . '';
			$html .= '</div>';
			$html .= '<a href="javascript:void(0)" onclick="zkmsg(' . $row ['id'] . ')" id="na_' . $row ['id'] . '" class="reds">【点击展开】</a></div></td>';
		} else {
			$html .= '<td class="msgtd">' . $row ['msg'] . '</td>';
		}
		$html .= '</tr>';
		$html .= '</table>';
	}
	_success ( $html, array ('total' => $total, 'page_total' => $mypage, 'page' => $page ) );
}

if($api == 'add_guestbook'){
	$username = trimMsg($_POST['username']);
	$address = trimMsg($_POST['address']);
	$phone = trimMsg($_POST['phone']);
	$msg = trimMsg(cn_substrR($_POST['content'], 1024), 1);
	$ip = GetIP();
	$dtime = time();
	
	if(empty($username) || empty($address) || empty($phone) || empty($msg)){
		_error('参数有误，请输入完整内容！');
	}
	$face = array('01','02','03','04','05','06','08','13','14','15','16','17','18','20');
	$rand = rand(1,count($face));
	$faces = $face[$rand];
	$query = "INSERT INTO `#@__guestbook`(title,tid,mid,uname,email,homepage,qq,face,msg,ip,dtime,ischeck,tel,address)
                  VALUES ('','0','0','$username','','','','$faces','$msg','$ip','$dtime','0','$phone', '$address'); ";
	$dsql->ExecuteNoneQuery($query);
	_success('留言成功！');
}

//添加留言
if ($api == 'addguestbook') {

}

function ajaxreturn($info, $status = 1, $data = array()) {
	$value = array ('info' => $info, 'status' => $status );
	exit ( json_encode ( array_merge ( $value, $data ) ) );
}

function _error($info, $data = array()) {
	ajaxreturn ( $info, 0, $data );
}

function _success($info, $data = array()) {
	ajaxreturn ( $info, 1, $data );
}
?>